package com.punai.config.sqlitedao;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.punai.common.core.text.Convert;
import com.punai.common.exception.ServiceException;
import com.punai.common.utils.sqlite.SqliteUtils;
import com.punai.config.constants.PnConfigConstants;
import com.punai.config.sqlitedomain.ConfigFeatureElement;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
public class ConfigFeatureElementRepository {

    @Autowired
    private SqliteUtils sqliteUtils;

    private final static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    public Page<ConfigFeatureElement> findPageByformat(Map<String, Object> inParams,
                                                    Pageable pageable) throws NumberFormatException, ParseException {
        int pageNumber = pageable.getPageNumber();
        int pageSize = pageable.getPageSize();
        int start = pageSize*(pageNumber - 1);
        String sql="SELECT element.* from tab_config_feature_element element "
                + "INNER JOIN tab_config_link_formatcategory_elem link "
                + "ON link.FEATURE_ID=element.id ";
        String where=" where 1=1 and (element.pid is null or trim(pid)='') ";
        if(MapUtil.isNotEmpty(inParams)){
            if(ObjectUtil.isNotNull(inParams.get("sreach"))){
                where=where+" and element.SREACH_KEY LIKE '%"+inParams.get("sreach").toString()+"%' ";
            }
            if(ObjectUtil.isNotNull(inParams.get("formatCategoryId"))){
                where=where+" and link.FORMATCATEGORY_ID = '"+inParams.get("formatCategoryId").toString()+"' ";
            }
            if(ObjectUtil.isNotNull(inParams.get("featureType"))){
                where=where+" and element.FEATURE_TYPE = '"+inParams.get("featureType").toString()+"' ";
            }
        }
        sql+=where+"ORDER BY link.sorting limit "+pageSize+" offset "+start;
        //initSqliteConnection();
        List<Map<String, Object>> list = sqliteUtils.list(sql);
        String countsql="SELECT count(*) as count FROM tab_config_feature_element element "
                + "INNER JOIN tab_config_link_formatcategory_elem link "
                + "ON link.FEATURE_ID=element.id "
                +where;
        Map countmap = sqliteUtils.unique(countsql);
        List<ConfigFeatureElement> newList = new ArrayList<>();
        for (Map<String, Object> map : list) {
            ConfigFeatureElement featureElement = new ConfigFeatureElement();
            featureElement.setId(map.get("ID")==null?null:map.get("ID").toString());
            featureElement.setName(map.get("NAME")==null?null:map.get("NAME").toString());
            featureElement.setCode(map.get("CODE")==null?null:map.get("CODE").toString());
            featureElement.setFeatureType(map.get("FEATURE_TYPE")==null?null:map.get("FEATURE_TYPE").toString());
            featureElement.setType(map.get("TYPE")==null?null:map.get("TYPE").toString());
            featureElement.setSelectCode(map.get("SELECT_CODE")==null?null:map.get("SELECT_CODE").toString());
            featureElement.setSelectLabel(map.get("SELECT_LABEL")==null?null:map.get("SELECT_LABEL").toString());
            featureElement.setOther(map.get("IS_OTHER")==null?null:map.get("IS_OTHER").toString().equals("Y"));
            featureElement.setHideCodes(map.get("HIDE_CODES")==null?null:map.get("HIDE_CODES").toString());
            featureElement.setCalculateCaliber(map.get("CALCULATE_CALIBER")==null?null:map.get("CALCULATE_CALIBER").toString().equals("Y"));
            String string =map.get("CREATE_DATE")==null?null: map.get("CREATE_DATE").toString();
            Date parse =string==null?null: string.contains("-")?simpleDateFormat.parse(string):new Date(Convert.toInt(string));
            featureElement.setCreateDate(parse);
            string = map.get("LAST_MODIFY")==null?null:map.get("LAST_MODIFY").toString();
            parse = string==null?null:string.contains("-")?simpleDateFormat.parse(string):new Date(Convert.toInt(string));
            featureElement.setLastModify(parse);
            featureElement.setEditState(map.get("EDIT_STATE")==null?null:Integer.parseInt(map.get("EDIT_STATE").toString()));
            featureElement.setSreachKey(map.get("SREACH_KEY")==null?null:map.get("SREACH_KEY").toString());
            featureElement.setVersion(map.get("VERSION")==null?null:Convert.toInt(map.get("VERSION").toString()));
            featureElement.setMustBe(map.get("IS_MUST_BE")==null?null:map.get("IS_MUST_BE").toString().equals("Y"));
            featureElement.setEngineeringType(map.get("ENGINEERING_TYPE")==null?null:map.get("ENGINEERING_TYPE").toString());
            featureElement.setCalculateCaliberType(map.get("CALCULATE_CALIBER_TYPE")==null?null:map.get("CALCULATE_CALIBER_TYPE").toString());
            featureElement.setPid(map.get("pid")==null?null:map.get("pid").toString());
            featureElement.setSum(map.get("is_sum")==null?null:map.get("is_sum").toString().equals("Y"));
            featureElement.setSearch(map.get("IS_SEARCH")==null?null:map.get("IS_SEARCH").toString().equals("Y"));
            featureElement.setChildren(findByPidFormat(featureElement.getId()));
            newList.add(featureElement);
        }
        return new PageImpl<>(newList, pageable, Integer.parseInt(countmap.get("count").toString()));
    }

    public ConfigFeatureElement findOne(String id) throws NumberFormatException, ParseException {
        ConfigFeatureElement featureElement = null;
        String sql="select * from tab_config_feature_element where id='"+id+"'";
        //initSqliteConnection();
        Map<String, Object> map = sqliteUtils.unique(sql);
        if(MapUtil.isNotEmpty(map)){
            featureElement = new ConfigFeatureElement();
            featureElement.setId(map.get("ID")==null?null:map.get("ID").toString());
            featureElement.setName(map.get("NAME")==null?null:map.get("NAME").toString());
            featureElement.setCode(map.get("CODE")==null?null:map.get("CODE").toString());
            featureElement.setFeatureType(map.get("FEATURE_TYPE")==null?null:map.get("FEATURE_TYPE").toString());
            featureElement.setType(map.get("TYPE")==null?null:map.get("TYPE").toString());
            featureElement.setSelectCode(map.get("SELECT_CODE")==null?null:map.get("SELECT_CODE").toString());
            featureElement.setSelectLabel(map.get("SELECT_LABEL")==null?null:map.get("SELECT_LABEL").toString());
            featureElement.setOther(map.get("IS_OTHER")==null?null:map.get("IS_OTHER").toString().equals("Y"));
            featureElement.setHideCodes(map.get("HIDE_CODES")==null?null:map.get("HIDE_CODES").toString());
            featureElement.setCalculateCaliber(map.get("CALCULATE_CALIBER")==null?null:map.get("CALCULATE_CALIBER").toString().equals("Y"));
            String string =map.get("CREATE_DATE")==null?null: map.get("CREATE_DATE").toString();
            Date parse =string==null?null: string.contains("-")?simpleDateFormat.parse(string):new Date(Convert.toInt(string));
            featureElement.setCreateDate(parse);
            string = map.get("LAST_MODIFY")==null?null:map.get("LAST_MODIFY").toString();
            parse = string==null?null:string.contains("-")?simpleDateFormat.parse(string):new Date(Convert.toInt(string));
            featureElement.setLastModify(parse);
            featureElement.setEditState(map.get("EDIT_STATE")==null?null:Integer.parseInt(map.get("EDIT_STATE").toString()));
            featureElement.setSreachKey(map.get("SREACH_KEY")==null?null:map.get("SREACH_KEY").toString());
            featureElement.setVersion(map.get("VERSION")==null?null:Convert.toInt(map.get("VERSION").toString()));
            featureElement.setMustBe(map.get("IS_MUST_BE")==null?null:map.get("IS_MUST_BE").toString().equals("Y"));
            featureElement.setEngineeringType(map.get("ENGINEERING_TYPE")==null?null:map.get("ENGINEERING_TYPE").toString());
            featureElement.setCalculateCaliberType(map.get("CALCULATE_CALIBER_TYPE")==null?null:map.get("CALCULATE_CALIBER_TYPE").toString());
            featureElement.setPid(map.get("pid")==null?null:StringUtil.isNullOrEmpty(map.get("pid").toString())?null:map.get("pid").toString());
            featureElement.setSum(map.get("is_sum")==null?null:map.get("is_sum").toString().equals("Y"));
            featureElement.setSearch(map.get("IS_SEARCH")==null?null:map.get("IS_SEARCH").toString().equals("Y"));
            featureElement.setParent(map.get("pid")==null?null: StringUtil.isNullOrEmpty(map.get("pid").toString())?null:findOne(map.get("pid").toString()));
        }
        return featureElement;
    }
    public List<ConfigFeatureElement> findAllOfFormat(Map<String, Object> searchMap) throws  NumberFormatException, ParseException {
        String sql="select * from tab_config_feature_element element "
                + "INNER JOIN tab_config_link_formatcategory_elem link "
                + "ON link.FEATURE_ID=element.id "
                + "where 1=1 ";
        if(searchMap.containsKey("formatCategoryId")){
            sql+="and link.FORMATCATEGORY_ID ='"+searchMap.get("formatCategoryId").toString()+"' ";
        }
        if(searchMap.containsKey("featuretype")){
            sql+="and element.FEATURE_TYPE ='"+searchMap.get("featuretype").toString()+"' ";
        }
        if(searchMap.containsKey("isSearch")){
            if(searchMap.get("isSearch").toString().length()==1){
                sql+="and element.IS_SEARCH ='"+searchMap.get("isSearch").toString()+"' ";
            }
            if(searchMap.get("isSearch").toString().length()>1){
                String a=searchMap.get("isSearch").toString().equals("true")?"Y":"N";
                sql+="and element.IS_SEARCH ='"+a+"' ";
            }
        }
        if(searchMap.containsKey("isCalculateCaliber")){
            sql+="and element.CALCULATE_CALIBER ='"+searchMap.get("isCalculateCaliber").toString()+"' ";
        }
        //initSqliteConnection();
        List<Map<String, Object>> list = sqliteUtils.list(sql);
        List<ConfigFeatureElement> newList = new ArrayList<ConfigFeatureElement>();
        for (Map<String, Object> map : list) {
            ConfigFeatureElement featureElement = new ConfigFeatureElement();
            featureElement.setId(map.get("ID")==null?null:map.get("ID").toString());
            featureElement.setName(map.get("NAME")==null?null:map.get("NAME").toString());
            featureElement.setCode(map.get("CODE")==null?null:map.get("CODE").toString());
            featureElement.setFeatureType(map.get("FEATURE_TYPE")==null?null:map.get("FEATURE_TYPE").toString());
            featureElement.setType(map.get("TYPE")==null?null:map.get("TYPE").toString());
            featureElement.setSelectCode(map.get("SELECT_CODE")==null?null:map.get("SELECT_CODE").toString());
            featureElement.setSelectLabel(map.get("SELECT_LABEL")==null?null:map.get("SELECT_LABEL").toString());
            featureElement.setOther(map.get("IS_OTHER")==null?null:map.get("IS_OTHER").toString().equals("Y"));
            featureElement.setHideCodes(map.get("HIDE_CODES")==null?null:map.get("HIDE_CODES").toString());
            featureElement.setCalculateCaliber(map.get("CALCULATE_CALIBER")==null?null:map.get("CALCULATE_CALIBER").toString().equals("Y"));
            String string =map.get("CREATE_DATE")==null?null: map.get("CREATE_DATE").toString();
            Date parse =string==null?null: string.contains("-")?simpleDateFormat.parse(string):new Date(Convert.toInt(string));
            featureElement.setCreateDate(parse);
            string = map.get("LAST_MODIFY")==null?null:map.get("LAST_MODIFY").toString();
            parse = string==null?null:string.contains("-")?simpleDateFormat.parse(string):new Date(Convert.toInt(string));
            featureElement.setLastModify(parse);
            featureElement.setEditState(map.get("EDIT_STATE")==null?null:Integer.parseInt(map.get("EDIT_STATE").toString()));
            featureElement.setSreachKey(map.get("SREACH_KEY")==null?null:map.get("SREACH_KEY").toString());
            featureElement.setVersion(map.get("VERSION")==null?null:Convert.toInt(map.get("VERSION").toString()));
            featureElement.setMustBe(map.get("IS_MUST_BE")==null?null:map.get("IS_MUST_BE").toString().equals("Y"));
            featureElement.setEngineeringType(map.get("ENGINEERING_TYPE")==null?null:map.get("ENGINEERING_TYPE").toString());
            featureElement.setCalculateCaliberType(map.get("CALCULATE_CALIBER_TYPE")==null?null:map.get("CALCULATE_CALIBER_TYPE").toString());
            featureElement.setPid(map.get("pid")==null?null:map.get("pid").toString());
            featureElement.setSum(map.get("is_sum")==null?null:map.get("is_sum").toString().equals("Y"));
            featureElement.setSearch(map.get("IS_SEARCH")==null?null:map.get("IS_SEARCH").toString().equals("Y"));
            newList.add(featureElement);
        }
        return newList;
    }
    public ConfigFeatureElement save(ConfigFeatureElement featureElement){
        String sql="insert into tab_config_feature_element("
                + "ID,CODE,FEATURE_TYPE,NAME,TYPE,SELECT_CODE,SELECT_LABEL,IS_OTHER,HIDE_CODES,"
                + "CALCULATE_CALIBER,CREATE_DATE,EDIT_STATE,LAST_MODIFY,SREACH_KEY,VERSION,"
                + "IS_MUST_BE,ENGINEERING_TYPE,CALCULATE_CALIBER_TYPE,pid,is_sum,IS_SEARCH) "
                + "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        //initSqliteConnection();
        featureElement.setEditState(PnConfigConstants.EDITSTATE.SAVESTATE.getType());
        featureElement.setVersion(PnConfigConstants.ENTITY_VERSION);
        featureElement.setSreachKey(featureElement.toString());
        featureElement.setId(IdUtil.fastUUID());
        Object[] args=new Object[]{
                featureElement.getId(),
                featureElement.getCode(),
                featureElement.getFeatureType(),
                featureElement.getName(),
                featureElement.getType(),
                featureElement.getSelectCode(),
                featureElement.getSelectLabel(),
                featureElement.getOther()==null?"N":featureElement.getOther()?"Y":"N",
                featureElement.getHideCodes(),
                featureElement.getCalculateCaliber()==null?"N":featureElement.getCalculateCaliber()?"Y":"N",
                simpleDateFormat.format(new Date()),
                featureElement.getEditState(),
                simpleDateFormat.format(new Date()),
                featureElement.getSreachKey(),
                featureElement.getVersion(),
                featureElement.getMustBe()==null?"N":featureElement.getMustBe()?"Y":"N",
                featureElement.getEngineeringType(),
                featureElement.getCalculateCaliberType(),
                featureElement.getPid(),
                featureElement.getSum()==null?"N":featureElement.getSum()?"Y":"N",
                featureElement.getSearch()==null?"N":featureElement.getSearch()?"Y":"N"
        };
        int rownumber= sqliteUtils.execute(sql,args);
        if(rownumber==0) {
            throw new ServiceException("sqlite.message.addException");
        }
        return featureElement;
    }
    public int delete(String id){
        List<String> deleteIds = new ArrayList<>();
        deleteIds.add(id);
        getChildrenIds(deleteIds,id);
        // 递归找 子
        String sql="delete from tab_config_feature_element where id IN('" + StrUtil.join("','",deleteIds) +"')";
        // todo 不删除关系么 ?
        int rownumber= sqliteUtils.execute(sql);
        if(rownumber==0) {
            throw new ServiceException("sqlite.message.addException");
        }
        return rownumber;
    }


    private void getChildrenIds(List<String> childIds,String pid) {
        String sql = "select id from tab_config_feature_element where pid = '" + pid + "'";
        List<ConfigFeatureElement> featureElements = sqliteUtils.selectListRecord(ConfigFeatureElement.class, sql);
        if (CollUtil.isNotEmpty(featureElements)) {
            for (ConfigFeatureElement featureElement : featureElements) {
                String id = featureElement.getId();
                childIds.add(id);
                getChildrenIds(childIds,id);
            }
        }
    }


    public ConfigFeatureElement update(ConfigFeatureElement featureElement) throws NumberFormatException, ParseException{
        ConfigFeatureElement findOne = this.findOne(featureElement.getId());
        featureElement.setEditState(PnConfigConstants.EDITSTATE.UPDATESTATE.getType());
        featureElement.setSreachKey(featureElement.toString());
        featureElement.setVersion(findOne.getVersion()+1);
        String sql="update tab_config_feature_element set ";
        if(StrUtil.isNotBlank(featureElement.getCode())){
            sql+="CODE='"+featureElement.getCode()+"',";
        }
        if(StrUtil.isNotBlank(featureElement.getFeatureType())){
            sql+="FEATURE_TYPE='"+featureElement.getFeatureType()+"',";
        }
        if(StrUtil.isNotBlank(featureElement.getName())){
            sql+="NAME='"+featureElement.getName()+"',";
        }
        if(StrUtil.isNotBlank(featureElement.getType())){
            sql+="TYPE='"+featureElement.getType()+"',";
        }
        if(StrUtil.isNotBlank(featureElement.getSelectCode())){
            sql+="SELECT_CODE='"+featureElement.getSelectCode()+"',";
        }else{
            if(!featureElement.getType().equals("select")){
                sql+="SELECT_CODE=null,";
            }
        }
        if(StrUtil.isNotBlank(featureElement.getSelectLabel())){
            sql+="SELECT_LABEL='"+featureElement.getSelectLabel()+"',";
        }else{
            if(!featureElement.getType().equals("select")){
                sql+="SELECT_LABEL=null,";
            }
        }
        String str=featureElement.getCalculateCaliber()?"Y":"N";
        sql+="CALCULATE_CALIBER='"+str+"',";
        sql+="LAST_MODIFY=?,";
        sql+="SREACH_KEY='"+ featureElement.toString() +"',";
        sql+="VERSION="+featureElement.getVersion()+",";
        str=featureElement.getMustBe()?"Y":"N";
        sql+="IS_MUST_BE='"+str+"',";
        str=featureElement.getSearch()?"Y":"N";
        sql+="IS_SEARCH='"+str+"',";
        if(StrUtil.isNotBlank(featureElement.getEngineeringType())){
            sql+="ENGINEERING_TYPE='"+featureElement.getEngineeringType()+"',";
        }
        if(StrUtil.isNotBlank(featureElement.getCalculateCaliberType())){
            sql+="CALCULATE_CALIBER_TYPE='"+featureElement.getCalculateCaliberType()+"',";
        }else{
            if(!featureElement.getCalculateCaliber()){
                sql+="CALCULATE_CALIBER_TYPE=NULL,";
            }
        }
        if(StrUtil.isNotBlank(featureElement.getPid())){
            sql+="pid='"+featureElement.getPid()+"',";
        }
        str=featureElement.getSum()?"Y":"N";
        sql+="is_sum='"+str+"'";
        sql+=" where id='"+findOne.getId()+"'";
        Object[] args=new Object[] {
                simpleDateFormat.format(new Date())
        };
        int rownumber= sqliteUtils.execute(sql,args);
        if(rownumber==0) {
            throw new ServiceException("sqlite.message.addException");
        }
        return featureElement;
    }
    public List<ConfigFeatureElement> findFeatureElementByEngineerClassCode(
            Map<String, Object> keymap) throws NumberFormatException, ParseException {
        String sql="SELECT "+
                "element.* "+
                "FROM tab_config_feature_element element "+
                "left join tab_config_link_eng_class_elem linkEngClass on element.id = linkEngClass.feature_Id "+
                "left join tab_config_engineer_class class on class.id=linkEngClass.class_Id "+
                "WHERE 1=1  ";
        if(keymap.containsKey("code")
                &&null!=keymap.get("code")
                &&StrUtil.isNotBlank(keymap.get("code").toString()))
            sql+="and class.code='"+keymap.get("code")+"' ";
        if(keymap.containsKey("featureType")
                &&null!=keymap.get("featureType")
                &&StrUtil.isNotBlank(keymap.get("featureType").toString()))
            sql+="and element.feature_Type='"+keymap.get("featureType")+"' ";
        if(keymap.containsKey("isSearch")){
            if(keymap.get("isSearch").toString().length()==1){
                sql+="and element.IS_SEARCH ='"+keymap.get("isSearch").toString()+"' ";
            }
            if(keymap.get("isSearch").toString().length()>1){
                String a=keymap.get("isSearch").toString().equals("true")?"Y":"N";
                sql+="and element.IS_SEARCH ='"+a+"' ";
            }
        }
        if(keymap.containsKey("engineeringType")
                &&null!=keymap.get("engineeringType")
                &&StrUtil.isNotBlank(keymap.get("engineeringType").toString()))
            sql+="and element.engineering_Type='"+keymap.get("engineeringType")+"' ";
        sql+="order by linkEngClass.SORTING";
        List<ConfigFeatureElement> ConfigFeatureElementlist = new ArrayList<ConfigFeatureElement>();
        //initSqliteConnection();
        List<Map<String, Object>> list = sqliteUtils.list(sql);
        for (Map<String, Object> map : list) {
            ConfigFeatureElement featureElement = new ConfigFeatureElement();
            featureElement.setId(map.get("ID")==null?null:map.get("ID").toString());
            featureElement.setName(map.get("NAME")==null?null:map.get("NAME").toString());
            featureElement.setCode(map.get("CODE")==null?null:map.get("CODE").toString());
            featureElement.setFeatureType(map.get("FEATURE_TYPE")==null?null:map.get("FEATURE_TYPE").toString());
            featureElement.setType(map.get("TYPE")==null?null:map.get("TYPE").toString());
            featureElement.setSelectCode(map.get("SELECT_CODE")==null?null:map.get("SELECT_CODE").toString());
            featureElement.setSelectLabel(map.get("SELECT_LABEL")==null?null:map.get("SELECT_LABEL").toString());
            featureElement.setOther(map.get("IS_OTHER")==null?null:map.get("IS_OTHER").toString().equals("Y"));
            featureElement.setHideCodes(map.get("HIDE_CODES")==null?null:map.get("HIDE_CODES").toString());
            featureElement.setCalculateCaliber(map.get("CALCULATE_CALIBER")==null?null:map.get("CALCULATE_CALIBER").toString().equals("Y"));
            String string =map.get("CREATE_DATE")==null?null: map.get("CREATE_DATE").toString();
            Date parse =string==null?null: string.contains("-")?simpleDateFormat.parse(string):new Date(Convert.toInt(string));
            featureElement.setCreateDate(parse);
            string = map.get("LAST_MODIFY")==null?null:map.get("LAST_MODIFY").toString();
            parse = string==null?null:string.contains("-")?simpleDateFormat.parse(string):new Date(Convert.toInt(string));
            featureElement.setLastModify(parse);
            featureElement.setEditState(map.get("EDIT_STATE")==null?null:Integer.parseInt(map.get("EDIT_STATE").toString()));
            featureElement.setSreachKey(map.get("SREACH_KEY")==null?null:map.get("SREACH_KEY").toString());
            featureElement.setVersion(map.get("VERSION")==null?null:Convert.toInt(map.get("VERSION").toString()));
            featureElement.setMustBe(map.get("IS_MUST_BE")==null?null:map.get("IS_MUST_BE").toString().equals("Y"));
            featureElement.setEngineeringType(map.get("ENGINEERING_TYPE")==null?null:map.get("ENGINEERING_TYPE").toString());
            featureElement.setCalculateCaliberType(map.get("CALCULATE_CALIBER_TYPE")==null?null:map.get("CALCULATE_CALIBER_TYPE").toString());
            featureElement.setPid(map.get("pid")==null?null:map.get("pid").toString());
            featureElement.setSum(map.get("is_sum")==null?null:map.get("is_sum").toString().equals("Y"));
            featureElement.setSearch(map.get("IS_SEARCH")==null?null:map.get("IS_SEARCH").toString().equals("Y"));
            ConfigFeatureElementlist.add(featureElement);
        }
        return ConfigFeatureElementlist;
    }
    public List<ConfigFeatureElement> findListByengineerClass(Map<String, Object> inParams) throws NumberFormatException, ParseException{
        String sql="SELECT element.* from tab_config_feature_element element "
                + "INNER JOIN tab_config_link_eng_class_elem link "
                + "ON link.FEATURE_ID=element.id "
                + "INNER JOIN tab_config_engineer_class engineer "
                + "ON link.CLASS_ID=engineer.id ";
        String where=" where 1=1 and (element.pid is null or trim(pid)='' )";
        if(MapUtil.isNotEmpty(inParams)){
            if(ObjectUtil.isNotNull(inParams.get("sreach"))){
                where=where+" and element.SREACH_KEY LIKE '%"+inParams.get("sreach").toString()+"%' ";
            }
            if(ObjectUtil.isNotNull(inParams.get("engineerClassId"))){
                where=where+" and engineer.id = '"+inParams.get("engineerClassId").toString()+"' ";
            }
            if(ObjectUtil.isNotNull(inParams.get("featureType"))){
                where=where+" and element.FEATURE_TYPE = '"+inParams.get("featureType").toString()+"' ";
            }
            if(ObjectUtil.isNotNull(inParams.get("engineerClassCode"))){
                where=where+" and engineer.code = '"+inParams.get("engineerClassCode").toString()+"' ";
            }
        }
        sql+=where+"ORDER BY link.sorting";
        //initSqliteConnection();
        List<Map<String, Object>> list = sqliteUtils.list(sql);
        List<ConfigFeatureElement> newList = new ArrayList<ConfigFeatureElement>();
        for (Map<String, Object> map : list) {
            ConfigFeatureElement featureElement = new ConfigFeatureElement();
            featureElement.setId(map.get("ID")==null?null:map.get("ID").toString());
            featureElement.setName(map.get("NAME")==null?null:map.get("NAME").toString());
            featureElement.setCode(map.get("CODE")==null?null:map.get("CODE").toString());
            featureElement.setFeatureType(map.get("FEATURE_TYPE")==null?null:map.get("FEATURE_TYPE").toString());
            featureElement.setType(map.get("TYPE")==null?null:map.get("TYPE").toString());
            featureElement.setSelectCode(map.get("SELECT_CODE")==null?null:map.get("SELECT_CODE").toString());
            featureElement.setSelectLabel(map.get("SELECT_LABEL")==null?null:map.get("SELECT_LABEL").toString());
            featureElement.setOther(map.get("IS_OTHER")==null?null:map.get("IS_OTHER").toString().equals("Y"));
            featureElement.setHideCodes(map.get("HIDE_CODES")==null?null:map.get("HIDE_CODES").toString());
            featureElement.setCalculateCaliber(map.get("CALCULATE_CALIBER")==null?null:map.get("CALCULATE_CALIBER").toString().equals("Y"));
            String string =map.get("CREATE_DATE")==null?null: map.get("CREATE_DATE").toString();
            Date parse =string==null?null: string.contains("-")?simpleDateFormat.parse(string):new Date(Convert.toInt(string));
            featureElement.setCreateDate(parse);
            string = map.get("LAST_MODIFY")==null?null:map.get("LAST_MODIFY").toString();
            parse = string==null?null:string.contains("-")?simpleDateFormat.parse(string):new Date(Convert.toInt(string));
            featureElement.setLastModify(parse);
            featureElement.setEditState(map.get("EDIT_STATE")==null?null:Integer.parseInt(map.get("EDIT_STATE").toString()));
            featureElement.setSreachKey(map.get("SREACH_KEY")==null?null:map.get("SREACH_KEY").toString());
            featureElement.setVersion(map.get("VERSION")==null?null:Convert.toInt(map.get("VERSION").toString()));
            featureElement.setMustBe(map.get("IS_MUST_BE")==null?null:map.get("IS_MUST_BE").toString().equals("Y"));
            featureElement.setEngineeringType(map.get("ENGINEERING_TYPE")==null?null:map.get("ENGINEERING_TYPE").toString());
            featureElement.setCalculateCaliberType(map.get("CALCULATE_CALIBER_TYPE")==null?null:map.get("CALCULATE_CALIBER_TYPE").toString());
            featureElement.setPid(map.get("pid")==null?null:map.get("pid").toString());
            featureElement.setSum(map.get("is_sum")==null?null:map.get("is_sum").toString().equals("Y"));
            featureElement.setSearch(map.get("IS_SEARCH")==null?null:map.get("IS_SEARCH").toString().equals("Y"));
            if(featureElement.getFeatureType().equals("4")||featureElement.getFeatureType().equals("0")){
                featureElement.setChildren(findByPid(featureElement.getId()));
            }
            newList.add(featureElement);
        }
        return newList;
    }
    public Page<ConfigFeatureElement> findPageByengineerClass(Map<String, Object> inParams,
                                                           Pageable pageable) throws NumberFormatException, ParseException{
        int pageNumber = pageable.getPageNumber() - 1;
        int pageSize = pageable.getPageSize();
        int start=pageSize*pageNumber;
        String sql="SELECT element.* from tab_config_feature_element element "
                + "INNER JOIN tab_config_link_eng_class_elem link "
                + "ON link.FEATURE_ID=element.id "
                + "INNER JOIN tab_config_engineer_class engineer "
                + "ON link.CLASS_ID=engineer.id ";
        String where=" where 1=1 and (element.pid is null or trim(pid)='' )";
        if(MapUtil.isNotEmpty(inParams)){
            if(ObjectUtil.isNotNull(inParams.get("sreach"))){
                where=where+" and element.SREACH_KEY LIKE '%"+inParams.get("sreach").toString()+"%' ";
            }
            if(ObjectUtil.isNotNull(inParams.get("engineerClassId"))){
                where=where+" and engineer.id = '"+inParams.get("engineerClassId").toString()+"' ";
            }
            if(ObjectUtil.isNotNull(inParams.get("featureType"))){
                where=where+" and element.FEATURE_TYPE = '"+inParams.get("featureType").toString()+"' ";
            }
            if(ObjectUtil.isNotNull(inParams.get("engineerClassCode"))){
                where=where+" and engineer.code = '"+inParams.get("engineerClassCode").toString()+"' ";
            }
        }
        sql+=where+"ORDER BY link.sorting limit "+pageSize+" offset "+start;
        //initSqliteConnection();
        List<Map<String, Object>> list = sqliteUtils.list(sql);
        String countsql="SELECT count(*) as count FROM tab_config_feature_element element "
                + "INNER JOIN tab_config_link_eng_class_elem link "
                + "ON link.FEATURE_ID=element.id "
                + "INNER JOIN tab_config_engineer_class engineer "
                + "ON link.CLASS_ID=engineer.id "+where;
        Map countmap = sqliteUtils.unique(countsql);
        List<ConfigFeatureElement> newList = new ArrayList<ConfigFeatureElement>();
        for (Map<String, Object> map : list) {
            ConfigFeatureElement featureElement = new ConfigFeatureElement();
            featureElement.setId(map.get("ID")==null?null:map.get("ID").toString());
            featureElement.setName(map.get("NAME")==null?null:map.get("NAME").toString());
            featureElement.setCode(map.get("CODE")==null?null:map.get("CODE").toString());
            featureElement.setFeatureType(map.get("FEATURE_TYPE")==null?null:map.get("FEATURE_TYPE").toString());
            featureElement.setType(map.get("TYPE")==null?null:map.get("TYPE").toString());
            featureElement.setSelectCode(map.get("SELECT_CODE")==null?null:map.get("SELECT_CODE").toString());
            featureElement.setSelectLabel(map.get("SELECT_LABEL")==null?null:map.get("SELECT_LABEL").toString());
            featureElement.setOther(map.get("IS_OTHER")==null?null:map.get("IS_OTHER").toString().equals("Y"));
            featureElement.setHideCodes(map.get("HIDE_CODES")==null?null:map.get("HIDE_CODES").toString());
            featureElement.setCalculateCaliber(map.get("CALCULATE_CALIBER")==null?null:map.get("CALCULATE_CALIBER").toString().equals("Y"));
            String string =map.get("CREATE_DATE")==null?null: map.get("CREATE_DATE").toString();
            Date parse =string==null?null: string.contains("-")?simpleDateFormat.parse(string):new Date(Convert.toInt(string));
            featureElement.setCreateDate(parse);
            string = map.get("LAST_MODIFY")==null?null:map.get("LAST_MODIFY").toString();
            parse = string==null?null:string.contains("-")?simpleDateFormat.parse(string):new Date(Convert.toInt(string));
            featureElement.setLastModify(parse);
            featureElement.setEditState(map.get("EDIT_STATE")==null?null:Integer.parseInt(map.get("EDIT_STATE").toString()));
            featureElement.setSreachKey(map.get("SREACH_KEY")==null?null:map.get("SREACH_KEY").toString());
            featureElement.setVersion(map.get("VERSION")==null?null:Convert.toInt(map.get("VERSION").toString()));
            featureElement.setMustBe(map.get("IS_MUST_BE")==null?null:map.get("IS_MUST_BE").toString().equals("Y"));
            featureElement.setEngineeringType(map.get("ENGINEERING_TYPE")==null?null:map.get("ENGINEERING_TYPE").toString());
            featureElement.setCalculateCaliberType(map.get("CALCULATE_CALIBER_TYPE")==null?null:map.get("CALCULATE_CALIBER_TYPE").toString());
            featureElement.setPid(map.get("pid")==null?null:map.get("pid").toString());
            featureElement.setSum(map.get("is_sum")==null?null:map.get("is_sum").toString().equals("Y"));
            featureElement.setSearch(map.get("IS_SEARCH")==null?null:map.get("IS_SEARCH").toString().equals("Y"));
            if(featureElement.getFeatureType().equals("4")||featureElement.getFeatureType().equals("0")){
                featureElement.setChildren(findByPid(featureElement.getId()));
            }
            newList.add(featureElement);
        }
        return new PageImpl<ConfigFeatureElement>(newList,pageable,Integer.parseInt(countmap.get("count").toString()));
    }
    public List<ConfigFeatureElement> findAll() throws NumberFormatException, ParseException {
        String sql="SELECT element.* from tab_config_feature_element element";
        //initSqliteConnection();
        List<Map<String, Object>> list = sqliteUtils.list(sql);
        List<ConfigFeatureElement> newList = new ArrayList<ConfigFeatureElement>();
        for (Map<String, Object> map : list) {
            ConfigFeatureElement featureElement = new ConfigFeatureElement();
            featureElement.setId(map.get("ID")==null?null:map.get("ID").toString());
            featureElement.setName(map.get("NAME")==null?null:map.get("NAME").toString());
            featureElement.setCode(map.get("CODE")==null?null:map.get("CODE").toString());
            featureElement.setFeatureType(map.get("FEATURE_TYPE")==null?null:map.get("FEATURE_TYPE").toString());
            featureElement.setType(map.get("TYPE")==null?null:map.get("TYPE").toString());
            featureElement.setSelectCode(map.get("SELECT_CODE")==null?null:map.get("SELECT_CODE").toString());
            featureElement.setSelectLabel(map.get("SELECT_LABEL")==null?null:map.get("SELECT_LABEL").toString());
            featureElement.setOther(map.get("IS_OTHER")==null?null:map.get("IS_OTHER").toString().equals("Y"));
            featureElement.setHideCodes(map.get("HIDE_CODES")==null?null:map.get("HIDE_CODES").toString());
            featureElement.setCalculateCaliber(map.get("CALCULATE_CALIBER")==null?null:map.get("CALCULATE_CALIBER").toString().equals("Y"));
            String string =map.get("CREATE_DATE")==null?null: map.get("CREATE_DATE").toString();
            Date parse =string==null?null: string.contains("-")?simpleDateFormat.parse(string):new Date(Convert.toInt(string));
            featureElement.setCreateDate(parse);
            string = map.get("LAST_MODIFY")==null?null:map.get("LAST_MODIFY").toString();
            parse = string==null?null:string.contains("-")?simpleDateFormat.parse(string):new Date(Convert.toInt(string));
            featureElement.setLastModify(parse);
            featureElement.setEditState(map.get("EDIT_STATE")==null?null:Integer.parseInt(map.get("EDIT_STATE").toString()));
            featureElement.setSreachKey(map.get("SREACH_KEY")==null?null:map.get("SREACH_KEY").toString());
            featureElement.setVersion(map.get("VERSION")==null?null:Convert.toInt(map.get("VERSION").toString()));
            featureElement.setMustBe(map.get("IS_MUST_BE")==null?null:map.get("IS_MUST_BE").toString().equals("Y"));
            featureElement.setEngineeringType(map.get("ENGINEERING_TYPE")==null?null:map.get("ENGINEERING_TYPE").toString());
            featureElement.setCalculateCaliberType(map.get("CALCULATE_CALIBER_TYPE")==null?null:map.get("CALCULATE_CALIBER_TYPE").toString());
            featureElement.setPid(map.get("pid")==null?null:map.get("pid").toString());
            featureElement.setSum(map.get("is_sum")==null?null:map.get("is_sum").toString().equals("Y"));
            featureElement.setSearch(map.get("IS_SEARCH")==null?null:map.get("IS_SEARCH").toString().equals("Y"));
            newList.add(featureElement);
        }
        return newList;
    }

    public List<ConfigFeatureElement> findByPidFormat(String pid) throws NumberFormatException, ParseException {
        String sql="SELECT "+
                "element.* "+
                "FROM tab_config_feature_element element "+
                "INNER JOIN tab_config_link_formatcategory_elem link "+
                "ON link.FEATURE_ID=element.id "+
                "WHERE element.pid='"+pid+"' ORDER BY link.sorting ";
        List<ConfigFeatureElement> ConfigFeatureElementlist = new ArrayList<ConfigFeatureElement>();
        //initSqliteConnection();
        List<Map<String, Object>> list = sqliteUtils.list(sql);
        for (Map<String, Object> map : list) {
            ConfigFeatureElement featureElement = new ConfigFeatureElement();
            featureElement.setId(map.get("ID")==null?null:map.get("ID").toString());
            featureElement.setName(map.get("NAME")==null?null:map.get("NAME").toString());
            featureElement.setCode(map.get("CODE")==null?null:map.get("CODE").toString());
            featureElement.setFeatureType(map.get("FEATURE_TYPE")==null?null:map.get("FEATURE_TYPE").toString());
            featureElement.setType(map.get("TYPE")==null?null:map.get("TYPE").toString());
            featureElement.setSelectCode(map.get("SELECT_CODE")==null?null:map.get("SELECT_CODE").toString());
            featureElement.setSelectLabel(map.get("SELECT_LABEL")==null?null:map.get("SELECT_LABEL").toString());
            featureElement.setOther(map.get("IS_OTHER")==null?null:map.get("IS_OTHER").toString().equals("Y"));
            featureElement.setHideCodes(map.get("HIDE_CODES")==null?null:map.get("HIDE_CODES").toString());
            featureElement.setCalculateCaliber(map.get("CALCULATE_CALIBER")==null?null:map.get("CALCULATE_CALIBER").toString().equals("Y"));
            String string =map.get("CREATE_DATE")==null?null: map.get("CREATE_DATE").toString();
            Date parse =string==null?null: string.contains("-")?simpleDateFormat.parse(string):new Date(Convert.toInt(string));
            featureElement.setCreateDate(parse);
            string = map.get("LAST_MODIFY")==null?null:map.get("LAST_MODIFY").toString();
            parse = string==null?null:string.contains("-")?simpleDateFormat.parse(string):new Date(Convert.toInt(string));
            featureElement.setLastModify(parse);
            featureElement.setEditState(map.get("EDIT_STATE")==null?null:Integer.parseInt(map.get("EDIT_STATE").toString()));
            featureElement.setSreachKey(map.get("SREACH_KEY")==null?null:map.get("SREACH_KEY").toString());
            featureElement.setVersion(map.get("VERSION")==null?null:Integer.parseInt(map.get("VERSION").toString()));
            featureElement.setMustBe(map.get("IS_MUST_BE")==null?null:map.get("IS_MUST_BE").toString().equals("Y"));
            featureElement.setEngineeringType(map.get("ENGINEERING_TYPE")==null?null:map.get("ENGINEERING_TYPE").toString());
            featureElement.setCalculateCaliberType(map.get("CALCULATE_CALIBER_TYPE")==null?null:map.get("CALCULATE_CALIBER_TYPE").toString());
            featureElement.setPid(map.get("pid")==null?null:map.get("pid").toString());
            featureElement.setSum(map.get("is_sum")==null?null:map.get("is_sum").toString().equals("Y"));
            featureElement.setSearch(map.get("IS_SEARCH")==null?null:map.get("IS_SEARCH").toString().equals("Y"));
            featureElement.setChildren(findByPidFormat(featureElement.getId()));
            ConfigFeatureElementlist.add(featureElement);
        }
        return ConfigFeatureElementlist;
    }

    // TODO 换了表名字  tab_config_link_formatcategory_elem ==> tab_config_link_eng_class_elem   536
    public List<ConfigFeatureElement> findByPid(String pid) throws NumberFormatException, ParseException {
        String sql="SELECT "+
                "element.* "+
                "FROM tab_config_feature_element element "+
                "INNER JOIN tab_config_link_eng_class_elem link "+
                "ON link.FEATURE_ID=element.id "+
                "WHERE element.pid='"+pid+"' ORDER BY link.sorting ";
        List<ConfigFeatureElement> ConfigFeatureElementlist = new ArrayList<ConfigFeatureElement>();
        //initSqliteConnection();
        List<Map<String, Object>> list = sqliteUtils.list(sql);
        for (Map<String, Object> map : list) {
            ConfigFeatureElement featureElement = new ConfigFeatureElement();
            featureElement.setId(map.get("ID")==null?null:map.get("ID").toString());
            featureElement.setName(map.get("NAME")==null?null:map.get("NAME").toString());
            featureElement.setCode(map.get("CODE")==null?null:map.get("CODE").toString());
            featureElement.setFeatureType(map.get("FEATURE_TYPE")==null?null:map.get("FEATURE_TYPE").toString());
            featureElement.setType(map.get("TYPE")==null?null:map.get("TYPE").toString());
            featureElement.setSelectCode(map.get("SELECT_CODE")==null?null:map.get("SELECT_CODE").toString());
            featureElement.setSelectLabel(map.get("SELECT_LABEL")==null?null:map.get("SELECT_LABEL").toString());
            featureElement.setOther(map.get("IS_OTHER")==null?null:map.get("IS_OTHER").toString().equals("Y"));
            featureElement.setHideCodes(map.get("HIDE_CODES")==null?null:map.get("HIDE_CODES").toString());
            featureElement.setCalculateCaliber(map.get("CALCULATE_CALIBER")==null?null:map.get("CALCULATE_CALIBER").toString().equals("Y"));
            String string =map.get("CREATE_DATE")==null?null: map.get("CREATE_DATE").toString();
            Date parse =string==null?null: string.contains("-")?simpleDateFormat.parse(string):new Date(Convert.toInt(string));
            featureElement.setCreateDate(parse);
            string = map.get("LAST_MODIFY")==null?null:map.get("LAST_MODIFY").toString();
            parse = string==null?null:string.contains("-")?simpleDateFormat.parse(string):new Date(Convert.toInt(string));
            featureElement.setLastModify(parse);
            featureElement.setEditState(map.get("EDIT_STATE")==null?null:Integer.parseInt(map.get("EDIT_STATE").toString()));
            featureElement.setSreachKey(map.get("SREACH_KEY")==null?null:map.get("SREACH_KEY").toString());
            featureElement.setVersion(map.get("VERSION")==null?null:Integer.parseInt(map.get("VERSION").toString()));
            featureElement.setMustBe(map.get("IS_MUST_BE")==null?null:map.get("IS_MUST_BE").toString().equals("Y"));
            featureElement.setEngineeringType(map.get("ENGINEERING_TYPE")==null?null:map.get("ENGINEERING_TYPE").toString());
            featureElement.setCalculateCaliberType(map.get("CALCULATE_CALIBER_TYPE")==null?null:map.get("CALCULATE_CALIBER_TYPE").toString());
            featureElement.setPid(map.get("pid")==null?null:map.get("pid").toString());
            featureElement.setSum(map.get("is_sum")==null?null:map.get("is_sum").toString().equals("Y"));
            featureElement.setSearch(map.get("IS_SEARCH")==null?null:map.get("IS_SEARCH").toString().equals("Y"));
            featureElement.setChildren(findByPid(featureElement.getId()));
            ConfigFeatureElementlist.add(featureElement);
        }
        return ConfigFeatureElementlist;
    }
    public ConfigFeatureElement findByCode(String code) throws NumberFormatException, ParseException {
        String sql="SELECT "+
                "element.* "+
                "FROM tab_config_feature_element element "+
                "WHERE element.code='"+code+"'";
        //initSqliteConnection();
        Map<String, Object> map = sqliteUtils.unique(sql);
        ConfigFeatureElement featureElement = null;
        if (MapUtil.isNotEmpty(map)) {
            featureElement = new ConfigFeatureElement();
            featureElement.setId(map.get("ID")==null?null:map.get("ID").toString());
            featureElement.setName(map.get("NAME")==null?null:map.get("NAME").toString());
            featureElement.setCode(map.get("CODE")==null?null:map.get("CODE").toString());
            featureElement.setFeatureType(map.get("FEATURE_TYPE")==null?null:map.get("FEATURE_TYPE").toString());
            featureElement.setType(map.get("TYPE")==null?null:map.get("TYPE").toString());
            featureElement.setSelectCode(map.get("SELECT_CODE")==null?null:map.get("SELECT_CODE").toString());
            featureElement.setSelectLabel(map.get("SELECT_LABEL")==null?null:map.get("SELECT_LABEL").toString());
            featureElement.setOther(map.get("IS_OTHER")==null?null:map.get("IS_OTHER").toString().equals("Y"));
            featureElement.setHideCodes(map.get("HIDE_CODES")==null?null:map.get("HIDE_CODES").toString());
            featureElement.setCalculateCaliber(map.get("CALCULATE_CALIBER")==null?null:map.get("CALCULATE_CALIBER").toString().equals("Y"));
            String string =map.get("CREATE_DATE")==null?null: map.get("CREATE_DATE").toString();
            Date parse =string==null?null: string.contains("-")?simpleDateFormat.parse(string):new Date(Convert.toInt(string));
            featureElement.setCreateDate(parse);
            string = map.get("LAST_MODIFY")==null?null:map.get("LAST_MODIFY").toString();
            parse = string==null?null:string.contains("-")?simpleDateFormat.parse(string):new Date(Convert.toInt(string));
            featureElement.setLastModify(parse);
            featureElement.setEditState(map.get("EDIT_STATE")==null?null:Integer.parseInt(map.get("EDIT_STATE").toString()));
            featureElement.setSreachKey(map.get("SREACH_KEY")==null?null:map.get("SREACH_KEY").toString());
            featureElement.setVersion(map.get("VERSION")==null?null:Convert.toInt(map.get("VERSION").toString()));
            featureElement.setMustBe(map.get("IS_MUST_BE")==null?null:map.get("IS_MUST_BE").toString().equals("Y"));
            featureElement.setEngineeringType(map.get("ENGINEERING_TYPE")==null?null:map.get("ENGINEERING_TYPE").toString());
            featureElement.setCalculateCaliberType(map.get("CALCULATE_CALIBER_TYPE")==null?null:map.get("CALCULATE_CALIBER_TYPE").toString());
            featureElement.setPid(map.get("pid")==null?null:map.get("pid").toString());
            featureElement.setSum(map.get("is_sum")==null?null:map.get("is_sum").toString().equals("Y"));
            featureElement.setSearch(map.get("IS_SEARCH")==null?null:map.get("IS_SEARCH").toString().equals("Y"));
            featureElement.setChildren(findByPid(featureElement.getId()));
        }
        return featureElement;
    }
    public List<ConfigFeatureElement> findAllOfEng(Map<String, Object> searchMap) throws NumberFormatException, ParseException {
        String sql="select * from tab_config_feature_element element "
                + "INNER JOIN tab_config_link_eng_class_elem link "
                + "ON link.FEATURE_ID=element.id "
                + "where 1=1 ";
        if(searchMap.containsKey("engineerClassId")){
            sql+="and link.CLASS_ID ='"+searchMap.get("engineerClassId").toString()+"' ";
        }
        if(searchMap.containsKey("calculateCaliberType")){
            sql+="and element.CALCULATE_CALIBER_TYPE ='"+searchMap.get("calculateCaliberType").toString()+"' ";
        }
        //initSqliteConnection();
        List<Map<String, Object>> list = sqliteUtils.list(sql);
        List<ConfigFeatureElement> newList = new ArrayList<ConfigFeatureElement>();
        for (Map<String, Object> map : list) {
            ConfigFeatureElement featureElement = new ConfigFeatureElement();
            featureElement.setId(map.get("ID")==null?null:map.get("ID").toString());
            featureElement.setName(map.get("NAME")==null?null:map.get("NAME").toString());
            featureElement.setCode(map.get("CODE")==null?null:map.get("CODE").toString());
            featureElement.setFeatureType(map.get("FEATURE_TYPE")==null?null:map.get("FEATURE_TYPE").toString());
            featureElement.setType(map.get("TYPE")==null?null:map.get("TYPE").toString());
            featureElement.setSelectCode(map.get("SELECT_CODE")==null?null:map.get("SELECT_CODE").toString());
            featureElement.setSelectLabel(map.get("SELECT_LABEL")==null?null:map.get("SELECT_LABEL").toString());
            featureElement.setOther(map.get("IS_OTHER")==null?null:map.get("IS_OTHER").toString().equals("Y"));
            featureElement.setHideCodes(map.get("HIDE_CODES")==null?null:map.get("HIDE_CODES").toString());
            featureElement.setCalculateCaliber(map.get("CALCULATE_CALIBER")==null?null:map.get("CALCULATE_CALIBER").toString().equals("Y"));
            String string =map.get("CREATE_DATE")==null?null: map.get("CREATE_DATE").toString();
            Date parse =string==null?null: string.contains("-")?simpleDateFormat.parse(string):new Date(Convert.toInt(string));
            featureElement.setCreateDate(parse);
            string = map.get("LAST_MODIFY")==null?null:map.get("LAST_MODIFY").toString();
            parse = string==null?null:string.contains("-")?simpleDateFormat.parse(string):new Date(Convert.toInt(string));
            featureElement.setLastModify(parse);
            featureElement.setEditState(map.get("EDIT_STATE")==null?null:Integer.parseInt(map.get("EDIT_STATE").toString()));
            featureElement.setSreachKey(map.get("SREACH_KEY")==null?null:map.get("SREACH_KEY").toString());
            featureElement.setVersion(map.get("VERSION")==null?null: Convert.toInt(map.get("VERSION").toString()));
            featureElement.setMustBe(map.get("IS_MUST_BE")==null?null:map.get("IS_MUST_BE").toString().equals("Y"));
            featureElement.setEngineeringType(map.get("ENGINEERING_TYPE")==null?null:map.get("ENGINEERING_TYPE").toString());
            featureElement.setCalculateCaliberType(map.get("CALCULATE_CALIBER_TYPE")==null?null:map.get("CALCULATE_CALIBER_TYPE").toString());
            featureElement.setPid(map.get("pid")==null?null:map.get("pid").toString());
            featureElement.setSum(map.get("is_sum")==null?null:map.get("is_sum").toString().equals("Y"));
            featureElement.setSearch(map.get("IS_SEARCH")==null?null:map.get("IS_SEARCH").toString().equals("Y"));
            newList.add(featureElement);
        }
        return newList;
    }
    public List<ConfigFeatureElement> findByPidOfengineerClass(String pid,Boolean isFindChild) throws NumberFormatException, ParseException {
        String sql="SELECT "+
                "element.* "+
                "FROM tab_config_feature_element element "+
                "INNER JOIN tab_config_link_eng_class_elem link "+
                "ON link.FEATURE_ID=element.id "+
                "WHERE element.pid='"+pid+"' ORDER BY link.sorting ";
        List<ConfigFeatureElement> ConfigFeatureElementlist = new ArrayList<ConfigFeatureElement>();
        //initSqliteConnection();
        List<Map<String, Object>> list = sqliteUtils.list(sql);
        for (Map<String, Object> map : list) {
            ConfigFeatureElement featureElement = new ConfigFeatureElement();
            featureElement.setId(map.get("ID")==null?null:map.get("ID").toString());
            featureElement.setName(map.get("NAME")==null?null:map.get("NAME").toString());
            featureElement.setCode(map.get("CODE")==null?null:map.get("CODE").toString());
            featureElement.setFeatureType(map.get("FEATURE_TYPE")==null?null:map.get("FEATURE_TYPE").toString());
            featureElement.setType(map.get("TYPE")==null?null:map.get("TYPE").toString());
            featureElement.setSelectCode(map.get("SELECT_CODE")==null?null:map.get("SELECT_CODE").toString());
            featureElement.setSelectLabel(map.get("SELECT_LABEL")==null?null:map.get("SELECT_LABEL").toString());
            featureElement.setOther(map.get("IS_OTHER")==null?null:map.get("IS_OTHER").toString().equals("Y"));
            featureElement.setHideCodes(map.get("HIDE_CODES")==null?null:map.get("HIDE_CODES").toString());
            featureElement.setCalculateCaliber(map.get("CALCULATE_CALIBER")==null?null:map.get("CALCULATE_CALIBER").toString().equals("Y"));
            String string =map.get("CREATE_DATE")==null?null: map.get("CREATE_DATE").toString();
            Date parse =string==null?null: string.contains("-")?simpleDateFormat.parse(string):new Date(Convert.toInt(string));
            featureElement.setCreateDate(parse);
            string = map.get("LAST_MODIFY")==null?null:map.get("LAST_MODIFY").toString();
            parse = string==null?null:string.contains("-")?simpleDateFormat.parse(string):new Date(Convert.toInt(string));
            featureElement.setLastModify(parse);
            featureElement.setEditState(map.get("EDIT_STATE")==null?null:Integer.parseInt(map.get("EDIT_STATE").toString()));
            featureElement.setSreachKey(map.get("SREACH_KEY")==null?null:map.get("SREACH_KEY").toString());
            featureElement.setVersion(map.get("VERSION")==null?null:Convert.toInt(map.get("VERSION").toString()));
            featureElement.setMustBe(map.get("IS_MUST_BE")==null?null:map.get("IS_MUST_BE").toString().equals("Y"));
            featureElement.setEngineeringType(map.get("ENGINEERING_TYPE")==null?null:map.get("ENGINEERING_TYPE").toString());
            featureElement.setCalculateCaliberType(map.get("CALCULATE_CALIBER_TYPE")==null?null:map.get("CALCULATE_CALIBER_TYPE").toString());
            featureElement.setPid(map.get("pid")==null?null:map.get("pid").toString());
            featureElement.setSum(map.get("is_sum")==null?null:map.get("is_sum").toString().equals("Y"));
            featureElement.setSearch(map.get("IS_SEARCH")==null?null:map.get("IS_SEARCH").toString().equals("Y"));
            if( isFindChild ) {
                featureElement.setChildren(findByPidOfengineerClass(featureElement.getId(),isFindChild));
            }
            ConfigFeatureElementlist.add(featureElement);
        }
        return ConfigFeatureElementlist;
    }
    public List<String> groupByPid() {
        List<String> pids = new ArrayList<String>();
        String sql="SELECT PID as id FROM tab_config_feature_element GROUP BY pid";
        List<Map<String, Object>> list = sqliteUtils.list(sql);
        for (Map<String, Object> map : list) {
            if(map.get("id")!=null&& StrUtil.isNotBlank(map.get("id").toString())){
                pids.add(map.get("id").toString());
            }
        }
        return pids;
    }


}
